package com.meteor.raft.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.meteor.raft.cluster.ClusterService;
import com.meteor.raft.core.RaftCore;
import com.meteor.raft.misc.WebUtils;
import com.meteor.raft.persistent.RaftData;
import com.meteor.raft.persistent.RaftPeer;
import com.meteor.raft.vo.PublishVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author gang.wang1903@xiaobao100.cn
 * @Date: 2019/7/22 10:04 AM
 */
@RestController
@RequestMapping("/meteorRaft/cluster")
@Slf4j
public class ClusterController {

    @Autowired
    private RaftCore raftCore;

    @Autowired
    private ClusterService clusterService;

    @Autowired
    private RaftData raftData;


    @PostMapping("/publish")
    public Boolean publish(HttpServletRequest request, HttpServletResponse response) {
        Boolean result = clusterService.publish(WebUtils.required(request, "key"),WebUtils.required(request, "value"));
        return result;
    }


    @PostMapping("/save")
    public Boolean save(HttpServletRequest request, HttpServletResponse response) {
        log.info("save leader peer data:");
        raftData.saveData(WebUtils.required(request, "key"),WebUtils.required(request, "value"));
        return true;
    }


    @GetMapping("/getData")
    public String getData(String key) {
        log.info("start get data:{}",key);
        String result = raftData.getData(key);
        return result;
    }


}
